<!DOCTYPE html><html><head> 
  <link href="../css/main.css" rel="stylesheet" type="text/css" /> 
  <script type="text/javascript" src="../MarkdownWebapp/MarkdownWebapp.nocache.js"></script> 
  <script type="text/javascript" src="../ace/ace.js"></script> 
  <link rel="icon" type="image/x-icon" href="../favicon.ico" /> 
  <script type="text/javascript">window["_GOOG_TRANS_EXT_VER"] = "1";</script> 
  <title>Cmd 向导 | 对编译器友好的代码原则</title> 
  <meta charset="UTF-8" /> 
  <meta name="viewport" content="width=device-width, initial-scale=1" /> 
  <script async="" type="text/javascript" src="../../../https@use.typekit.com/bqo8qzg.js"></script> 
  <script type="text/javascript">

    function showMenu(link_obj){
      var menu = document.getElementById("tpl-toc");

      link_obj.innerHTML = (link_obj.innerHTML == 'CLOSE MENU') ? 'MENU' : 'CLOSE MENU';

      if(menu.style.display == 'block'){
        menu.style.display = 'none';
      }else{
        menu.style.display = 'block';
      }
    }

  </script> 
 </head> 
 <body> 
  <div id="header"> 
   <a href="../../../www.sencha.com/default.htm"><img class="sencha-logo" src="../images/logo.png" /></a> 
   <h1 id="current-product"><a href="#">Cmd 向导 ▼</a></h1> 
   <div id="mobile_menu"> 
    <a id="mobile_menu_link" onclick="showMenu(this);" href="#">MENU</a> 
   </div> 
    
  </div> 
  <div id="content" itemscope="" itemtype="http://schema.org/Article"> 
   <div id="tpl-toc"> 
    <div id="tpl-toc-content"> 
     <a class="toggle-button" href="#">expand</a> 
     <ul> 
      <li class="file"><a href="../../default.htm" data-full-load="true" title="Home">Sencha 向导</a></li> 
      <li class="file"><a href="index.html" data-full-load="true" title="Cmd Home">Sencha Cmd 向导</a></li> 
      <li class="file"><a href="whats_new_cmd.html" title="">Cmd 6 新特性</a></li> 
      <li class="file"><a href="fashion.html" title="">欢迎使用 Fashion</a></li> 
      <li class="file"><a href="cmd_upgrade_guide.html" title="Sencha Cmd 6 - 升级向导">Sencha Cmd 6 - 升级向导</a></li> 
      <li class="file"><a href="intro_to_cmd.html" title="Sencha Cmd 介绍">Sencha Cmd 介绍</a></li> 
      <li class="file"><a href="cmd_upgrade.html" title="理解 App 升级">理解 App 升级</a></li> 
      <li class="file"><a href="cmd_compiler.html" title="对编译器友好的代码原则">对编译器友好的代码原则</a></li> 
      <li class="file"><a href="cordova_phonegap.html" title="Cordova 和 Phonegap 应用">Cordova 和 Phonegap 应用</a></li> 
      <li class="file"><a href="workspaces.html" title="Sencha Cmd 的 Workspace">Sencha Cmd 的 Workspace</a></li> 
      <li class="file"><a href="microloader.html" title="加载器 Microloader">加载器 Microloader</a></li> 
      <li class="file"><a href="resource_management.html" title="资源管理">资源管理</a><span class="tpl-toc-img-new">★</span></li> 
      <li class="file"><a href="compatibility_matrix.html" title="Sencha Cmd 兼容性列表">Sencha Cmd 兼容性列表</a><span class="tpl-toc-img-new">★</span></li> 
      <li class="folder"><a href="#">Sencha Cmd 包(Packages)</a> 
       <ul expand="false"> 
        <li class="file"><a href="cmd_packages/cmd_packages.html" title="Sencha Cmd 包(Packages)">Sencha Cmd 包(Packages)</a></li> 
        <li class="file"><a href="cmd_packages/cmd_creating_packages.html" title="创建包(Packages)">创建包(Packages)</a></li> 
       </ul></li> 
      <li class="folder"><a href="#">Sencha Cmd 进阶</a> 
       <ul expand="false"> 
        <li class="file"><a href="advanced_cmd/cmd_advanced.html" title="Sencha Cmd 进阶">Sencha Cmd 进阶</a></li> 
        <li class="file"><a href="advanced_cmd/cmd_build.html" title="App Build 过程内部机制">App Build 过程内部机制</a></li> 
        <li class="file"><a href="advanced_cmd/cmd_compiler_reference.html" title="Sencha 编译器 参考">Sencha 编译器 参考</a></li> 
        <li class="file"><a href="advanced_cmd/cmd_ant.html" title="Ant 集成">Ant 集成</a></li> 
        <li class="file"><a href="advanced_cmd/cmd_metadata.html" title="Sencha Cmd Metadata">Sencha Cmd Metadata</a></li> 
        <li class="file"><a href="advanced_cmd/cmd_reference.html" title="Sencha Cmd 参考">Sencha Cmd 参考</a></li> 
       </ul></li> 
      <li class="folder"><a href="#">Ext JS</a> 
       <ul expand="false"> 
        <li class="file"><a href="extjs/cmd_app.html" title="">Sencha Cmd 结合 Ext JS</a></li> 
       </ul></li> 
      <li class="folder"><a href="#">Sencha Touch</a> 
       <ul expand="false"> 
        <li class="file"><a href="touch/cmd_app.html" title="">Sencha Cmd 结合 Sencha Touch</a></li> 
       </ul></li> 
      <li class="file"><a href="sencha_blog.html" title="Sencha 博客">Sencha 博客</a></li> 
     </ul> 
    </div> 
   </div> 
   <div id="middle"> 
    <div id="tpl-content"> 
     <div> 
      <div class="toc-page" style="display:block;">
        Contents 
       <a class="toc-page-h2" href="#Prerequisites" title="Prerequisites">Prerequisites</a> 
       <a class="toc-page-h2" href="#What_The_Compiler_Is_Not" title="What The Compiler Is Not">What The Compiler Is Not</a> 
       <a class="toc-page-h2" href="#Framework_Awareness" title="Framework Awareness">Framework Awareness</a> 
       <a class="toc-page-h2" href="#Code_Organization" title="Code Organization">Code Organization</a> 
       <a class="toc-page-h2" href="#Class_Declaration" title="Class Declaration">Class Declaration</a> 
       <a class="toc-page-h3" href="#Standard_Form" title="Standard Form">Standard Form</a> 
       <a class="toc-page-h3" href="#Wrapped_Function_Forms" title="Wrapped Function Forms">Wrapped Function Forms</a> 
       <a class="toc-page-h4" href="#Function_Form" title="Function Form">Function Form</a> 
       <a class="toc-page-h4" href="#Called_Function_Form" title="Called Function Form">Called Function Form</a> 
       <a class="toc-page-h4" href="#Called_Parenthesized_Function_Form" title="Called-Parenthesized Function Form">Called-Parenthesized Function Form</a> 
       <a class="toc-page-h4" href="#Parenthesized_Called_Function_Form" title="Parenthesized-Called Function Form">Parenthesized-Called Function Form</a> 
       <a class="toc-page-h3" href="#Keywords" title="Keywords">Keywords</a> 
       <a class="toc-page-h4" href="#Keywords_using_String" title="Keywords using String">Keywords using String</a> 
       <a class="toc-page-h4" href="#Keywords_using_String_or_String__" title="Keywords using String or String[]">Keywords using String or String[]</a> 
       <a class="toc-page-h4" href="#Forms_of__code_mixins__code_" title="Forms of &lt;code&gt;mixins&lt;/code&gt;">Forms of <code>mixins</code></a> 
       <a class="toc-page-h4" href="#The__code_statics__code__Keyword" title="The &lt;code&gt;statics&lt;/code&gt; Keyword">The <code>statics</code> Keyword</a> 
       <a class="toc-page-h4" href="#The__code_singleton__code__Keyword" title="The &lt;code&gt;singleton&lt;/code&gt; Keyword">The <code>singleton</code> Keyword</a> 
       <a class="toc-page-h2" href="#Overrides" title="Overrides">Overrides</a> 
       <a class="toc-page-h3" href="#Standard_Override_Form" title="Standard Override Form">Standard Override Form</a> 
       <a class="toc-page-h3" href="#Use_Cases" title="Use Cases">Use Cases</a> 
       <a class="toc-page-h4" href="#Overrides_as_Patches" title="Overrides as Patches">Overrides as Patches</a> 
       <a class="toc-page-h5" href="#Naming_Recommendation_" title="Naming Recommendation:">Naming Recommendation:</a> 
       <a class="toc-page-h4" href="#Overrides_as_Partial_Classes" title="Overrides as Partial Classes">Overrides as Partial Classes</a> 
       <a class="toc-page-h4" href="#Overrides_as_Aspects" title="Overrides as Aspects">Overrides as Aspects</a> 
       <a class="toc-page-h3" href="#Using__code_requires__code__and__code_uses__code__in_an_Override" title="Using &lt;code&gt;requires&lt;/code&gt; and &lt;code&gt;uses&lt;/code&gt; in an Override">Using <code>requires</code> and <code>uses</code> in an Override</a> 
       <a class="toc-page-h3" href="#Using__code_callParent__code__and__code_callSuper__code_" title="Using &lt;code&gt;callParent&lt;/code&gt; and &lt;code&gt;callSuper&lt;/code&gt;">Using <code>callParent</code> and <code>callSuper</code></a> 
       <a class="toc-page-h3" href="#Override_Compatibility" title="Override Compatibility">Override Compatibility</a> 
       <a class="toc-page-h2" href="#Conclusion" title="Conclusion">Conclusion</a> 
       <a class="toc-page-h2" href="#Next_Steps" title="Next Steps">Next Steps</a> 
      </div> 
      <div id="tpl-content-name" itemprop="name"> 
       <h1>对编译器友好的代码原则</h1> 
      </div> 
      <div id="tpl-content-articleBody" itemprop="articleBody"> 
       <p>One of the major components in <a href="../../../www.sencha.com/products/sencha-cmd/default.htm">Sencha Cmd</a> is its compiler. This guide describes how to write code that gets the most out of the compiler and prepares for future framework-aware optimizations.</p> 
       <h2 id="Prerequisites">Prerequisites</h2> 
       <p>The following guides are recommended reading before proceeding further:</p> 
       <ul> 
        <li><a href="intro_to_cmd.html">Sencha Cmd 介绍</a></li> 
        <li><a href="extjs/cmd_app.html">Using Sencha Cmd</a></li> 
       </ul> 
       <h2 id="What_The_Compiler_Is_Not">What The Compiler Is Not</h2> 
       <p>Sencha Cmd compiler is <strong>not</strong> a replacement for tools like these:</p> 
       <ul> 
        <li><a href="../../../yui.github.io/yuicompressor/default.htm">YUI Compressor</a></li> 
        <li><a href="../../../https@developers.google.com/closure/compiler/default.htm">Google Closure Compiler</a></li> 
        <li><a href="../../../https@github.com/mishoo/UglifyJS/default.htm">UglifyJS</a></li> 
       </ul> 
       <p>These tools solve different problems for JavaScript developers and are very good at the world of JavaScript, but have no understanding of Sencha framework features such as <code>Ext.define</code> for declaring classes.</p> 
       <h2 id="Framework_Awareness">Framework Awareness</h2> 
       <p>The role of the Sencha Cmd compiler is to provide framework-aware optimizations and diagnostics. Once code has passed through the Sencha Cmd compiler, it is ready for more general tools.</p> 
       <p>These kinds of optimizations have shown to significantly improve the “ingest” time of JavaScript code by the browser, especially on legacy browsers.</p> 
       <p>For the compiler to provide these benefits, however, it is now important to look at the coding conventions that the compiler can “understand” and therefore optimize for you. Following the conventions described in this guide ensure that your code is positioned to get the most from Sencha Cmd today and in the future.</p> 
       <h2 id="Code_Organization">Code Organization</h2> 
       <p>The dynamic loader and the previous JSBuilder have always made certain assumptions about how classes are organized, but they were not seriously impacted by failure to follow those guidelines. These guidelines are very similar to Java.</p> 
       <p>To recap, these guidelines are:</p> 
       <ul> 
        <li>Each JavaScript source file should contain one <code>Ext.define</code> statement at global scope.</li> 
        <li>The name of a source file matches the last segment of the name of the defined type such as the name of the source file containing <code>Ext.define(&quot;MyApp.foo.bar.Thing&quot;, ...</code> is “Thing.js”.</li> 
        <li>All source files are stored in a folder structure that is based on the namespace of the defined type. For example, given <code>Ext.define(&quot;MyApp.foo.bar.Thing&quot;, ...</code>, the source file is in a path ending with “/foo/bar”.</li> 
       </ul> 
       <p>Internally, the compiler views source files and classes as basically synonymous. It makes no attempt to split up files to remove classes that are not required. Only complete files are selected and included in the output. This means that if any class in a source file is required, all classes in the file will be included in the output.</p> 
       <p>To give the compiler the freedom to select code at the class-level, it is essential to put only one class in each file.</p> 
       <h2 id="Class_Declaration">Class Declaration</h2> 
       <p>The Sencha 类系统 provides the <code>Ext.define</code> function to enable high-level, object oriented programming. The compiler takes the view that <code>Ext.define</code> is really a form of “declarative” programming and processes the “class declaration” accordingly.</p> 
       <p>Clearly if <code>Ext.define</code> is understood as a declaration, the content of the class body cannot be constructed dynamically in code. While this practice is rare, it is valid JavaScript. But as we shall see below in the code forms, this is antithetical to the compiler’s ability to understand the code it parses. Dynamic class declarations are often used to do things that are better handled by other features of the compiler. For more on these features, see the <a href="advanced_cmd/cmd_compiler_reference.html">Sencha 编译器 参考</a>.</p> 
       <p>The compiler understands these “keywords” of this declarative language:</p> 
       <ul> 
        <li><code>requires</code></li> 
        <li><code>uses</code></li> 
        <li><code>extend</code></li> 
        <li><code>mixins</code></li> 
        <li><code>statics</code></li> 
        <li><code>alias</code></li> 
        <li><code>singleton</code></li> 
        <li><code>override</code></li> 
        <li><code>alternateClassName</code></li> 
        <li><code>xtype</code></li> 
       </ul> 
       <p>For the compiler to recognize your class declarations, they need to follow one of the following forms.</p> 
       <h3 id="Standard_Form">Standard Form</h3> 
       <p>Most classes use simple declarations like this:</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('Foo.bar.Thing', {
    // keywords go here ... such as:

    extend: '...',

    // ...
});</code></pre> 
       <p>The second argument is the class body which is processed by the compiler as the class “declaration”.</p> 
       <p><strong>Note:</strong> <em>In all forms, call <code>Ext.define</code> at global scope.</em> </p> 
       <h3 id="Wrapped_Function_Forms">Wrapped Function Forms</h3> 
       <p>In some use cases the class declaration is wrapped in a function to create a closure scope for the class methods. In all of the various forms, it is critical for the compiler that the function end with a <code>return</code> statement that returns the class body as an object literal. Other techniques are not recognized by the compiler.</p> 
       <h4 id="Function_Form">Function Form</h4> 
       <p>To streamline the older forms of this technique described below, <code>Ext.define</code> understands that if given a function as its second argument, that it should invoke that function to produce the class body. It also passes the reference to the class as the single argument to facilitate access to static members via the closure scope. Internally to the framework, this was the most common reason for the closure scope.</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('Foo.bar.Thing', function (Thing) {

    return {
        // keywords go here ... such as:

        extend: '...',

        // ...
    };
});</code></pre> 
       <p><strong>Note:</strong> <em>This form is only supported in Ext JS 4.1.2 and later and Sencha Touch 2.1 and later.</em></p> 
       <h4 id="Called_Function_Form">Called Function Form</h4> 
       <p>In previous releases, the “Function Form” was not supported, so the function was simply invoked immediately:</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('Foo.bar.Thing', function () {

    return {
        // keywords go here ... such as:

        extend: '...',

        // ...
    };
}());</code></pre> 
       <h4 id="Called_Parenthesized_Function_Form">Called-Parenthesized Function Form</h4> 
       <p>This form and the next are commonly used to appease tools like JSHint (or JSLint).</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('Foo.bar.Thing', (function () {

    return {
        // keywords go here ... such as:

        extend: '...',

        // ...
    };
})());</code></pre> 
       <h4 id="Parenthesized_Called_Function_Form">Parenthesized-Called Function Form</h4> 
       <p>Another variation on immediately called “Function Form” to appease JSHint/JSLint.</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('Foo.bar.Thing', (function () {

    return {
        // keywords go here ... such as:

        extend: '...',

        // ...
    };
}()));</code></pre> 
       <h3 id="Keywords">Keywords</h3> 
       <p>The class declaration in its many forms ultimately contains “keywords”. Each keyword has its own semantics, but there are many that have a common “shape”.</p> 
       <h4 id="Keywords_using_String">Keywords using String</h4> 
       <p>The <code>extend</code> and <code>override</code> keywords only accept a string literal.</p> 
       <p>These keywords are also mutually exclusive in that only one can be used in any declaration.</p> 
       <h4 id="Keywords_using_String_or_String__">Keywords using String or String[]</h4> 
       <p>The following keywords all have the same form:</p> 
       <ul> 
        <li><code>requires</code></li> 
        <li><code>uses</code></li> 
        <li><code>alias</code></li> 
        <li><code>alternateClassName</code></li> 
        <li><code>xtype</code></li> 
       </ul> 
       <p>The supported forms for these keywords are as follows.</p> 
       <p>Just a string:</p> 
       <pre mode="javascript" theme="chrome"><code>requires: 'Foo.thing.Bar',
//...</code></pre> 
       <p>An array of strings:</p> 
       <pre mode="javascript" theme="chrome"><code>requires: [ 'Foo.thing.Bar', 'Foo.other.Thing' ],
//...</code></pre> 
       <h4 id="Forms_of__code_mixins__code_">Forms of <code>mixins</code></h4> 
       <p>Using an object literal, the name given the mixin can be quoted or not:</p> 
       <pre mode="javascript" theme="chrome"><code>mixins: {
    name: 'Foo.bar.Mixin',
    'other': 'Foo.other.Mixin'
},
//...</code></pre> 
       <p>Mixins can also be specified as a String[]:</p> 
       <pre mode="javascript" theme="chrome"><code>mixins: [
    'Foo.bar.Mixin',
    'Foo.other.Mixin'
],
//...</code></pre> 
       <p>This approach relies on the <code>mixinId</code> of the mixin class but also allows the receiving class to control the mixin order. This is important if the mixins have overlapping methods or properties and the receiving class wants to control which mixin supplies the overlapping methods or properties.</p> 
       <h4 id="The__code_statics__code__Keyword">The <code>statics</code> Keyword</h4> 
       <p>This keyword places properties or methods on the class, as opposed to on each of the instances. This must be an object literal.</p> 
       <pre mode="javascript" theme="chrome"><code>statics: {
    // members go here
},
// ...</code></pre> 
       <h4 id="The__code_singleton__code__Keyword">The <code>singleton</code> Keyword</h4> 
       <p>This keyword was historically only used with a boolean “true” value:</p> 
       <pre mode="javascript" theme="chrome"><code>singleton: true,</code></pre> 
       <p>The following (redundant) use is also supported:</p> 
       <pre mode="javascript" theme="chrome"><code>singleton: false,</code></pre> 
       <h2 id="Overrides">Overrides</h2> 
       <p>In Ext JS 4.1.0 and Sencha Touch 2.0, <code>Ext.define</code> gained the ability to manage overrides. Historically, overrides have been used to patch code to work around bugs or add enhancements. This use was complicated with the introduction of the dynamic loader because of the timing required to execute the <code>Ext.override</code> method. Also, in large applications with many overrides, not all overrides in the code base were needed by all pages or builds (for example, if the target class was not required).</p> 
       <p>All this changed once the class system and loader understood overrides. This trend only continues with Sencha Cmd. The compiler understands overrides and their dependency effects and load-sequence issues.</p> 
       <p>In the future, the compiler will become even more aggressive at dead-code elimination of methods replaced by an override. Using managed overrides as described below enables this optimization of your code once it’s available in Sencha Cmd.</p> 
       <h3 id="Standard_Override_Form">Standard Override Form</h3> 
       <p>Below is the standard form of an override. The choice of namespace is somewhat arbitrary, but see below for suggestions.</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('MyApp.patches.grid.Panel', {
    override: 'Ext.grid.Panel',

    ...
});</code></pre> 
       <h3 id="Use_Cases">Use Cases</h3> 
       <p>With the ability to use <code>Ext.define</code> to manage overrides, new idioms have opened up and are actively being leveraged. For example in the code generators of <a href="../../../www.sencha.com/products/architect/default.htm">Sencha Architect</a> and internal to the framework, that break apart large classes like <code>Ext.Element</code> into more manageable and cohesive pieces.</p> 
       <h4 id="Overrides_as_Patches">Overrides as Patches</h4> 
       <p>Overrides as patches are the historical use case and hence the most common in practice today.</p> 
       <p><strong>Caution:</strong> Take care when patching code. While the use of override itself is supported, the end result of overriding framework methods is not supported. All overrides should be carefully reviewed whenever upgrading to a new framework version.</p> 
       <p>That said, it is, at times, necessary to override framework methods. The most common case for this to fix a bug. The Standard Override Form is ideal in this case. In fact, Sencha Support will at times provide customer with patches in this form. Once provided, however, managing such patches and removing them when no longer needed, is a matter for the review process previously mentioned.</p> 
       <h5 id="Naming_Recommendation_">Naming Recommendation:</h5> 
       <ul> 
        <li><p>Organize patches in a namespace associated with the top-level namespace of the target. For example, “MyApp.patches” targets the “Ext” namespace. If third party code is involved then perhaps another level or namespace should be chosen to correspond to its top-level namespace. From there, name the override using a matching name and sub-namespace. In the previous example:</p><p><code>Ext -&gt; MyApp.patches).grid.Panel</code></p></li> 
       </ul> 
       <h4 id="Overrides_as_Partial_Classes">Overrides as Partial Classes</h4> 
       <p>When dealing with code generation (as in Sencha Architect), it is common for a class to consist of two parts: one machine generated and one human edited. In some languages, there is formal support for the notion of a “partial class” or a class-in-two-parts.</p> 
       <p>Using an override, you can manage this cleanly:</p> 
       <p>In <code>./foo/bar/Thing.js</code>:</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('Foo.bar.Thing', {
    // NOTE: This class is generated - DO NOT EDIT...

    requires: [
        'Foo.bar.custom.Thing'
    ],

    method: function () {
        // some generated method
    },

    ...
});</code></pre> 
       <p>In <code>./foo/bar/custom/Thing.js</code>:</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('Foo.bar.custom.Thing', {
    override: 'Foo.bar.Thing',

    method: function () {
        this.callParent(); // calls generated method
        ...
    },

    ...
});</code></pre> 
       <p>Naming Recommendations:</p> 
       <ul> 
        <li>Organize generated vs. hand-edited code by namespace.</li> 
        <li>If not by namespace, consider a common base name with a suffix on one or the other, such as <code>Foo.bar.ThingOverride</code> or <code>Foo.bar.ThingGenerated</code> so that the parts of a class collate together in listings.</li> 
       </ul> 
       <h4 id="Overrides_as_Aspects">Overrides as Aspects</h4> 
       <p>A common problem for base classes in object-oriented designs is the “fat base class”. This happens because some behaviors apply across all classes. When these behaviors (or features) are not needed, however, they cannot be readily removed if they are implemented as part of some large base class.</p> 
       <p>Using overrides, these features can be collected in their own hierarchy and then <code>requires</code> can be used to select these features when needed.</p> 
       <p>In <code>./foo/feature/Component.js</code>:</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('Foo.feature.Component', {
    override: 'Ext.Component',

    ...
});</code></pre> 
       <p>In <code>./foo/feature/grid/Panel.js</code>:</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('Foo.feature.grid.Panel', {
    override: 'Ext.grid.Panel',

    requires: [
        'Foo.feature.Component' // since overrides do not &quot;extend&quot; each other
    ],

    ...
});</code></pre> 
       <p>This feature can be used now by requiring it:</p> 
       <pre mode="javascript" theme="chrome"><code>...
requires: [
    'Foo.feature.grid.Panel'
]</code></pre> 
       <p>Or with a proper “bootstrap” file (see <a href="workspaces.html">Sencha Cmd 的 Workspace</a></p> 
       <pre mode="javascript" theme="chrome"><code>...
requires: [
    'Foo.feature.*'
]</code></pre> 
       <p>Naming Recommendation:</p> 
       <ul> 
        <li>Organize generated vs. hand-edited code by namespace. This enables use of wildcards to bring in all aspects of the feature.</li> 
       </ul> 
       <h3 id="Using__code_requires__code__and__code_uses__code__in_an_Override">Using <code>requires</code> and <code>uses</code> in an Override</h3> 
       <p>These keywords are supported in overrides. Use of <code>requires</code> may limit the compiler’s ability to reorder the code of an override.</p> 
       <h3 id="Using__code_callParent__code__and__code_callSuper__code_">Using <code>callParent</code> and <code>callSuper</code></h3> 
       <p>To support all of these new uses cases, <code>callParent</code> was enhanced in Ext JS 4.0 and Sencha Touch 2.0 to “call the next method”. The “next method” may be an overridden method or an inherited method. As long as there is a next method, <code>callParent</code> will call it.</p> 
       <p>Another way to view this is that <code>callParent</code> works the same for all forms of <code>Ext.define</code>, be they classes or overrides.</p> 
       <p>While this helped in some areas, it unfortunately made bypassing the original method (as a patch or bug fix) more difficult. Ext JS 4.1 and later and Sencha Touch 2.1 and later provides a method named <code>callSuper</code> that can bypass an overridden method.</p> 
       <p>In future releases, the compiler will use this semantic difference to perform dead-code elimination of overridden methods.</p> 
       <h3 id="Override_Compatibility">Override Compatibility</h3> 
       <p>Starting in version 4.2.2, overrides can declare their <code>compatibility</code> based on the framework version or on versions of other packages. This can be useful for selectively applying patches that are safely ignored when they are incompatible with the target class version.</p> 
       <p>The simplest use case is to test framework version for compatibility:</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('App.overrides.grid.Panel', {
    override: 'Ext.grid.Panel',

    compatibility: '4.2.2', // only if framework version is 4.2.2

    //...
});</code></pre> 
       <p>An array is treated as an OR, so if any specs match, the override is compatible.</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('App.overrides.some.Thing', {
    override: 'Foo.some.Thing',

    compatibility: [
        '4.2.2',
        'foo@1.0.1-1.0.2'
    ],

    //...
});</code></pre> 
       <p>To require that all specifications match, an object can be provided:</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('App.overrides.some.Thing', {
    override: 'Foo.some.Thing',

    compatibility: {
        and: [
            '4.2.2',
            'foo@1.0.1-1.0.2'
        ]
    },

    //...
});</code></pre> 
       <p>Because the object form is just a recursive check, these can be nested:</p> 
       <pre mode="javascript" theme="chrome"><code>Ext.define('App.overrides.some.Thing', {
    override: 'Foo.some.Thing',

    compatibility: {
        and: [
            '4.2.2',  // exactly version 4.2.2 of the framework *AND*
            {
                // either (or both) of these package specs:
                or: [
                    'foo@1.0.1-1.0.2',
                    'bar@3.0+'
                ]
            }
        ]
    },

    //...
});</code></pre> 
       <p>For details on version syntax, see the <code>checkVersion</code> method of <code>Ext.Version</code>.</p> 
       <h2 id="Conclusion">Conclusion</h2> 
       <p>As Sencha Cmd continues to evolve, it continues to introduce new diagnostic messages to help point out deviations from these guidelines.</p> 
       <p>A good place to start is to see how this information can help inform your own internal code style guidelines and practices.</p> 
       <h2 id="Next_Steps">Next Steps</h2> 
       <ul> 
        <li><a href="advanced_cmd/cmd_compiler_reference.html">Sencha 编译器 参考</a></li> 
        <li><a href="advanced_cmd/cmd_advanced.html">Sencha Cmd 进阶</a></li> 
       </ul> 
      </div> 
      <div class="tpl-content-lastUpdated">
        Last updated 
       <div id="tpl-content-lastUpdated-date" itemprop="datePublished" datetime="2016-04-01T13:04Z">
         April 1, 2016 
       </div> 
      </div> 
     </div> 
    </div> 
   </div> 
  </div> 
  <div id="footer"> 
   <div id="footer-social"> 
    <div class="g-plusone" data-size="medium" data-annotation="none"></div> 
   </div> 
   <div id="footer-content"> 
    <a href="../../../www.sencha.com/products">Products</a> | 
    <a href="../../../www.sencha.com/services">Services</a> | 
    <a href="../../../www.sencha.com/support">Support</a> | 
    <a href="../../../www.sencha.com/training">Training</a> | 
    <a href="../../../www.sencha.com/partners">Partners</a> | 
    <a href="../../../www.sencha.com/blog">Community</a> | 
    <a href="../../../www.sencha.com/company">Company</a> 
   </div> 
  </div> 
  <div id="menu-product-container" style="display:none;"> 
   <div id="menu-dropdown"> 
    <div class="menu-landing-products"> 
     <div id="menu-vertical"> 
      <p class="menu-landing-product" rel="cmd"> <a> Sencha Cmd </a> </p> 
      <p class="menu-landing-product" rel="extjs"> <a> Sencha Ext JS </a> </p> 
      <p class="menu-landing-product" rel="sencha_test"> <a> Sencha Test </a> </p> 
      <p class="menu-landing-product" rel="ide_plugins"> <a> Sencha IDE Plugins </a> </p> 
      <p class="menu-landing-product" rel="sencha_inspector"> <a> Sencha Inspector </a> </p> 
      <p class="menu-landing-product" rel="gxt"> <a> Sencha GXT </a> </p> 
      <p class="menu-landing-product" rel="webappmgr"> <a> Sencha Web Application Manager </a> </p> 
      <p class="menu-landing-product" rel="architect"> <a> Sencha Architect </a> </p> 
      <p class="menu-landing-product" rel="touch"> <a> Sencha Touch </a> </p> 
     </div> 
    </div> 
    <div id="menu-product-details"> 
     <div id="cmd-menu-product" class="menu-specific-product"> 
      <div class="menu-landing-doc-heading">
        Sencha Cmd 
      </div> 
      <div class="menu-landing-doc-heading">
        6.x 
      </div> 
      <a href="../6.x"> 6.x </a> 
      <span class="tpl-toc-img-new">★</span> 
      <br /> 
      <div class="menu-landing-doc-heading">
        5.x 
      </div> 
      <a href="../5.x"> 5.x </a> 
     </div> 
     <div id="extjs-menu-product" class="menu-specific-product"> 
      <div class="menu-landing-doc-heading">
        Sencha Ext JS 
      </div> 
      <div class="menu-landing-doc-heading">
        6.x 
      </div> 
      <a href="../../extjs/6.0/default.htm"> 6.0.2 </a> 
      <a class="menu-landing-zip" style="padding-right:0;" href="../../downloads/extjs-docs-6.0.2-classic.zip"> (classic) </a> 
      <a class="menu-landing-zip menu-landing-lesspad" href="../../downloads/extjs-docs-6.0.2-modern.zip"> (modern) </a> 
      <span class="tpl-toc-img-new">★</span> 
      <br /> 
      <a href="../../extjs/6.0/default.htm"> 6.0.1 </a> 
      <a class="menu-landing-zip" style="padding-right:0;" href="../../downloads/extjs-docs-6.0.1-classic.zip"> (classic) </a> 
      <a class="menu-landing-zip menu-landing-lesspad" href="../../downloads/extjs-docs-6.0.1-modern.zip"> (modern) </a> 
      <span class="tpl-toc-img-new">★</span> 
      <br /> 
      <a href="../../extjs/6.0/default.htm"> 6.0.0 </a> 
      <a class="menu-landing-zip" style="padding-right:0;" href="../../downloads/extjs-docs-6.0.0-classic.zip"> (classic) </a> 
      <a class="menu-landing-zip" style="padding-left:0;" href="../../downloads/extjs-docs-6.0.0-modern.zip"> (modern) </a> 
      <div class="menu-landing-doc-heading">
        5.1.x 
      </div> 
      <a href="../../extjs/5.1/default.htm"> 5.1.2 </a> 
      <a class="menu-landing-zip" href="../../downloads/extjs-docs-5.1.2.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../extjs/5.1/default.htm"> 5.1.1 </a> 
      <a class="menu-landing-zip" href="../../downloads/extjs-docs-5.1.1.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../extjs/5.1/default.htm"> 5.1.0 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/ext-docs-5.1.0.zip"> (offline docs) </a> 
      <br /> 
      <div class="menu-landing-doc-heading">
        5.0.x 
      </div> 
      <a href="../../extjs/5.0/default.htm"> 5.0.1 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/ext-docs-5.0.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../extjs/5.0/default.htm"> 5.0.0 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/ext-docs-5.0.0.zip"> (offline docs) </a> 
      <br /> 
      <div class="menu-landing-doc-heading">
        4.2.x 
      </div> 
      <a href="../../extjs/4.2.5/default.htm"> 4.2.5 </a> 
      <a class="menu-landing-zip" href="../../downloads/extjs-docs-4.2.5.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../extjs/4.2.4/default.htm"> 4.2.4 </a> 
      <a class="menu-landing-zip" href="../../downloads/extjs-docs-4.2.4.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../extjs/4.2.3/default.htm"> 4.2.3 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/extjs-docs-4.2.3.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../extjs/4.2.2/default.htm"> 4.2.2 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/extjs-docs-4.2.2.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../extjs/4.2.1/default.htm"> 4.2.1 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/extjs-docs-4.2.1.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../extjs/4.2.0/default.htm"> 4.2.0 </a> 
      <br /> 
      <div class="menu-landing-doc-heading">
        4.1.x 
      </div> 
      <a href="../../extjs/4.1.3/default.htm"> 4.1.3 </a> 
      <br /> 
      <a href="../../extjs/4.1.2/default.htm"> 4.1.2 </a> 
      <br /> 
      <a href="../../extjs/4.1.1/default.htm"> 4.1.1 </a> 
      <br /> 
      <a href="../../extjs/4.1.0/default.htm"> 4.1.0 </a> 
      <br /> 
      <div class="menu-landing-doc-heading">
        4.0.x 
      </div> 
      <a href="../../extjs/4.0.7/default.htm"> 4.0.7 </a> 
      <br /> 
      <a href="../../extjs/4.0.0/default.htm"> 4.0.0 </a> 
     </div> 
     <div id="sencha_test-menu-product" class="menu-specific-product"> 
      <div class="menu-landing-doc-heading">
        Sencha Test 
      </div> 
      <a href="../../sencha_test/index.html"> Sencha Test </a> 
      <span class="tpl-toc-img-new">★</span> 
     </div> 
     <div id="ide_plugins-menu-product" class="menu-specific-product"> 
      <div class="menu-landing-doc-heading">
        Sencha IDE Plugins 
      </div> 
      <a href="../../ide_plugins/jetbrains_ide_plugin.html"> JetBrains Plugin 6.x </a> 
      <span class="tpl-toc-img-new">★</span> 
      <br /> 
      <a href="../../ide_plugins/eclipse_ide_plugin.html"> Eclipse Plugin </a> 
      <span class="tpl-toc-img-new">★</span> 
      <br /> 
      <a href="../../ide_plugins/visual_studio_ide_plugin.html"> Visual Studio Plugin </a> 
      <span class="tpl-toc-img-new">★</span> 
     </div> 
     <div id="sencha_inspector-menu-product" class="menu-specific-product"> 
      <div class="menu-landing-doc-heading">
        Sencha Inspector 
      </div> 
      <a href="../../sencha_inspector/index.html"> Sencha Inspector 6.x </a> 
      <span class="tpl-toc-img-new">★</span> 
     </div> 
     <div id="gxt-menu-product" class="menu-specific-product"> 
      <div class="menu-landing-doc-heading">
        Sencha GXT 
      </div> 
      <!-- GXT 4.0 --> 
      <div class="menu-landing-doc-heading">
        4.0 
       <span class="tpl-toc-img-new">★</span> 
      </div> 
      <a href="../../../examples.sencha.com/gxt/4.0.1"> 4.0 Explorer Examples </a> 
      <br /> 
      <a href="../../../examples.sencha.com/gxt-edash/4.0.1"> 4.0 Dashboard Example </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/gxt/examples-src/gxt-edash-4.0.1.zip"> (War) </a> 
      <br /> 
      <a href="../../gxt/4.x/default.htm"> 4.0 Guides </a> 
      <br /> 
      <a href="../../gxt/4.x/javadoc/gxt-4.0.0"> 4.0 Javadoc </a> 
      <br /> 
      <!-- GXT 3.1.x --> 
      <div class="menu-landing-doc-heading">
        3.1 
      </div> 
      <a href="../../../www.sencha.com/examples/default.htm"> 3.1.4 Examples </a> 
      <br /> 
      <a href="../../gxt/3.x/default.htm"> 3.1.4 Guides </a> 
      <br /> 
      <a href="../../gxt/3.x/javadoc"> 3.1.4 Javadoc </a> 
      <br /> 
      <!-- GXT 3.0.x --> 
      <!-- GXT 2.x --> 
      <div class="menu-landing-doc-heading">
        2.0 
      </div> 
      <a href="../../../https@www.sencha.com/examples-2/default.htm"> 2.3.1 Examples </a> 
     </div> 
     <div id="webappmgr-menu-product" class="menu-specific-product"> 
      <div class="menu-landing-doc-heading">
        Sencha Web Application Manager 
      </div> 
      <div class="menu-landing-doc-heading">
        6.0.x 
      </div> 
      <a href="../../webappmgr"> 6.0.0 </a> 
      <span class="tpl-toc-img-new">★</span> 
     </div> 
     <div id="architect-menu-product" class="menu-specific-product"> 
      <div class="menu-landing-doc-heading">
        Sencha Architect 
      </div> 
      <div class="menu-landing-doc-heading">
        3.x 
      </div> 
      <a href="../../architect/3.5/default.htm"> 3.5 </a> 
      <span class="tpl-toc-img-new">★</span> 
      <br /> 
      <a href="../../architect/3/default.htm"> 3.2 </a> 
      <a class="menu-landing-zip menu-landing-lesspad" href="../../../cdn.sencha.com/downloads/docs/architect-docs-3.zip"> (offline docs) </a> 
     </div> 
     <div id="touch-menu-product" class="menu-specific-product"> 
      <div class="menu-landing-doc-heading">
        Sencha Touch 
      </div> 
      <div class="menu-landing-doc-heading">
        2.4.x 
      </div> 
      <a href="../../touch/2.4/default.htm"> 2.4.2 </a> 
      <span class="tpl-toc-img-new">★</span> 
      <a class="menu-landing-zip menu-landing-lesspad" href="../../downloads/touch-docs-2.4.2.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../touch/2.4/default.htm"> 2.4.1 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/touch-docs-2.4.1.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../touch/2.4/2.4.0-apidocs"> 2.4.0 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/touch-docs-2.4.0.zip"> (offline docs) </a> 
      <div class="menu-landing-doc-heading">
        2.3.x 
      </div> 
      <a href="../../touch/2.3.1/default.htm"> 2.3.1 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/touch-docs-2.3.1.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../touch/2.3.0/default.htm"> 2.3.0 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/touch-docs-2.3.0.zip"> (offline docs) </a> 
      <br /> 
      <div class="menu-landing-doc-heading">
        2.2.x 
      </div> 
      <a href="../../touch/2.2.1/default.htm"> 2.2.1 </a> 
      <a class="menu-landing-zip" href="../../../cdn.sencha.com/downloads/docs/touch-docs-2.2.1.zip"> (offline docs) </a> 
      <br /> 
      <a href="../../touch/2.2.0/default.htm"> 2.2.0 </a> 
      <br /> 
      <div class="menu-landing-doc-heading">
        2.1.x 
      </div> 
      <a href="../../touch/2.1.1/default.htm"> 2.1.1 </a> 
      <br /> 
      <a href="../../touch/2.1.0/default.htm"> 2.1.0 </a> 
      <br /> 
      <div class="menu-landing-doc-heading">
        2.0.x 
      </div> 
      <a href="../../touch/2.0.2/default.htm"> 2.0.2 </a> 
      <br /> 
      <a href="../../touch/2.0.1/default.htm"> 2.0.1 </a> 
      <br /> 
      <a href="../../touch/2.0.0/default.htm"> 2.0.0 </a> 
     </div> 
    </div> 
   </div> 
  </div> 
   
  <script type="text/javascript">
    adroll_adv_id = "5RM2BL5IGRFLRG5VDQ7OIE";
    adroll_pix_id = "LWO7UZXBFVESTOMYFI2LYS";
    /* OPTIONAL: provide email to improve user identification */
    /* adroll_email = "username_40example.com"; */
    (function () {
      var _onload = function(){
        if (document.readyState && !/loaded|complete/.test(document.readyState)){setTimeout(_onload, 10);return}
        if (!window.__adroll_loaded){__adroll_loaded=true;setTimeout(_onload, 50);return}
        var scr = document.createElement("script");
        var host = (("https:" == document.location.protocol) ? "../../../https@s.adroll.com" : "../../../a.adroll.com");
        scr.setAttribute('async', 'true');
        scr.type = "text/javascript";
        scr.src = host + "/j/roundtrip.js";
        ((document.getElementsByTagName('head') || [null])[0] ||
        document.getElementsByTagName('script')[0].parentNode).appendChild(scr);
      };
      if (window.addEventListener) {window.addEventListener('load', _onload, false);}
      else {window.attachEvent('onload', _onload)}
    }());
  </script> 
  <script>
    var menu = document.getElementById("tpl-toc"),
        link_obj = document.getElementById("mobile_menu_link"),
        links = menu.querySelectorAll('a');

    for(i=0; i < links.length; i++) {
      links[i].onclick = function() {
        var mq = window.matchMedia('only screen and (max-width: 768px)');

        if(mq.matches) {
          menu.style.display = 'none';
          link_obj.innerHTML = 'MENU';
        }
      }
    }
  </script> 
  <script type="text/javascript">
    </script>              <script src='../../../munchkin.marketo.net/munchkin-beta.js' type='text/javascript'></script><script>Munchkin.init('374-WES-121');</script>   
 
</body></html>